package com.company.eos.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.company.eos.common.util.PageUtils;
import com.company.eos.common.R;
import com.company.eos.controller.form.audit.AuditTaskForm;
import com.company.eos.controller.form.audit.DeleteAuditForm;
import com.company.eos.controller.form.audit.SearchAuditContentForm;
import com.company.eos.controller.form.audit.SearchAuditTaskByPageForm;
import com.company.eos.service.AuditDetailService;
import com.company.eos.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.HashMap;

/**
 * 任务审批Controller
 *
 * @author Song, Wei
 * Date: 2022/4/28 13:30
 */
@Slf4j
@RestController
@RequestMapping("/audit")
@Tag(name = "任务审批Web接口")
public class AuditController {

    @Autowired
    private AuditDetailService auditDetailService;
    @Autowired
    private UserService userService;

    @PostMapping("/detailList")
    @Operation(summary = "查询审批任务分页列表")
    @SaCheckPermission(mode = SaMode.OR)
    public R getAuditDetailList(@Valid @RequestBody SearchAuditTaskByPageForm form) {
        log.info("查询审批任务分页列表，参数：{}", form);
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        @NotNull(message = "page不能为空") @Min(value = 1, message = "page不能小于1") Integer page = form.getPage();
        @NotNull(message = "length不能为空") @Range(min = 10, max = 50, message = "length必须在10~50之间") Integer length = form.getLength();
        int start = (page - 1) * length;
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        param.put("start", start);
        param.put("myId", loginIdAsInt);
        // 1表示请假，2表示报销，3表示会议，4表示离职
        if ("会议申请".equals(form.getType())) {
            param.put("type", 3);
        } else if ("员工请假".equals(form.getType())) {
            param.put("type", 1);
        } else if ("员工离职".equals(form.getType())) {
            param.put("type", 4);
        } else if ("报销申请".equals(form.getType())) {
            param.put("type", 2);
        }
        PageUtils pageUtils = auditDetailService.searchTaskByPage(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/auditContent")
    @Operation(summary = "查询审批任务详细内容")
    @SaCheckPermission(mode = SaMode.OR)
    public R getAuditContent(@Valid @RequestBody SearchAuditContentForm form) {
        log.info("查询审批任务详细内容，参数：{}", form);
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        int userId = StpUtil.getLoginIdAsInt();
        param.put("userId", userId);
        param.put("role", userService.searchUserRoles(userId));
        HashMap content = auditDetailService.searchAuditContent(param);
        return R.ok().put("content", content);
    }

    @PostMapping("/auditTask")
    @Operation(summary = "审批任务")
    @SaCheckPermission(mode = SaMode.OR)
    public R auditTask(@Valid @RequestBody AuditTaskForm form) {
        log.info("审批员工申请，参数：{}", form);
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        int userId = StpUtil.getLoginIdAsInt();
        param.put("userId", userId);
        param.put("role", userService.searchUserRoles(userId));
        auditDetailService.auditTask(param);
        return R.ok();
    }

    @PostMapping("/deleteAudit")
    @Operation(summary = "删除审批记录")
    @SaCheckPermission(mode = SaMode.OR)
    public R auditTask(@Valid @RequestBody DeleteAuditForm form) {
        log.info("删除审批记录，参数：{}", form);
        int flow = Integer.parseInt(form.getFlowId());
        int i = auditDetailService.deleteAudit(flow);
        return R.ok().put("rows", i);
    }

}
